Skip to content

Create an implicit part for a Voice outside a Staff - #197

Open
wattdave wants to merge 2 commits into
frescobaldi:masterfrom
wattdave:fix-voice-addlyrics-crash
Open

wattdave wants to merge 2 commits into
frescobaldi:masterfrom
wattdave:fix-voice-addlyrics-crash

Conversation

@wattdave

Copy link
Copy Markdown

Builds on the \addlyrics fix (#196). This PR is based on that branch, so it also contains that commit; please review #196 first.

Problem

A \new Voice placed directly in a group has an implicit Staff in LilyPond:

\score {
  \new StaffGroup <<
    \new Voice = "S" { \set Staff.instrumentName = #"Cantus" c''2 d'' }
    \addlyrics { Du tout }
    \new Voice = "B" { c2 g, }
  >>
}

ly musicxml exports no parts at all for this. In a real four-voice CPDL score,
where the Voices use note variables and \set Staff.instrumentName, it also
prints 'NoneType' object has no attribute 'name'.

Root cause

In ParseSource.check_context() (ly/musicxml/lymus2musxml.py), the Voice
branch only opens a new section. No part is created unless a Staff context
does it. Because nothing ever adds the section to a part, the music is dropped,
and Mediator.set_partname() writes to self.part, which is None.

Fix

When a Voice context starts with no part open, create one with
mediator.new_part(). When that Voice ends, add its music to the part with
mediator.check_part(), the same way a Staff is handled. A new implicit_staff
stack tracks this for nested Voices.

Tests

  • Added tests/test_xml_files/voice_in_group.{ly,xml} and test_voice_in_group.
  • Before the fix, the test fails because the output has no parts. After the fix,
    the full suite gives 20 passed and 2 xfailed. The 2 xfails were already there.
  • On the full Brumel "Du tout plongiet" CPDL score (convert-ly'd to 2.26), the
    output went from an empty document to 4 parts with notes and lyrics.

Not addressed

  • LayoutContext and Keyword warnings: the handlers for \layout/\midi
    blocks are not implemented. These warnings are harmless.
  • Measure boundaries are still wrong for this score, also with Fix bar counting and markup placement in MusicXML export #193 merged on top (tested: it merges cleanly, but the output is unchanged). Long notes that cross a barline in 2/2 are not split, so measures come out with the wrong length. That looks like a separate issue.
  • Incipits written with \cadenzaOn, and bar checks used where there are no
    barlines, still produce measures of the wrong length.

🤖 Generated with Claude Code

wattdave and others added 2 commits September 26, 2026 21:41
Lyrics following music via \addlyrics were appended as plain lists
directly into the barlist of the current part, so iterating the part
crashed with "AttributeError: 'list' object has no attribute 'pickup'".

Collect \addlyrics into a LyricsSection and merge it into the preceding
music when it ends, as is already done for \lyricsto. This also makes
the lyrics appear in the exported MusicXML.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A \new Voice placed directly in a StaffGroup (or other group) has an
implicit Staff in LilyPond, but the MusicXML export never created a
part for it. Its music was dropped, and a \set Staff.instrumentName
inside it failed with "'NoneType' object has no attribute 'name'".

When a Voice context starts with no part open, create a new part and
add the voice to it when the Voice ends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
wang1zhen pushed a commit to wang1zhen/python-ly that referenced this pull request Oct 2, 2026
LilyPond creates a Staff for a Voice that is not inside one. The export
put such voices in no part (an empty score), in the part of the staff
before them, or in one part together. Since errors in node handlers are
no longer hidden, a \set Staff.instrumentName in such a voice even
raised an AttributeError.

Now a Voice that starts while no part is open gets a part, and its
music is added to that part when the Voice ends. Sections added to a
part stay findable by name, so \lyricsto still finds the voice.

A top-level music expression without \score (e.g. \new Staff << ... >>)
is now handled itself, not only its children, so the voices in it are
recognized as being inside a Staff.

The test input with a StaffGroup of voices is from frescobaldi#197, which
addresses the same problem.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CMyxTwm2w6VtbxBdt5dL1T
@wang1zhen

Copy link
Copy Markdown

Hi @wattdave, I noticed it overlaps with a stack of MusicXML export PRs I opened (#199 → #202 → #200 → #201), and wanted to flag it so we don't duplicate work.

This PR (#197): #202 now also gives a Voice outside any Staff a part of its own. I added your voice_in_group.ly input to its tests, with credit to this PR in the commit message. Both approaches create a part when a Voice starts with no part open. The differences in #202:

  • When such a voice ends, its music goes directly into the part. It does not merge the voice into the enclosing section first, which with << \new Voice … \new Voice … >> put both voices into one part.
  • A top-level expression without \score (e.g. \new Staff << \new Voice … >>) is handled itself, so the voices in it are seen as being inside a staff.
  • \lyricsto still finds a voice by name after its music has moved into the part.

#196: #201 implements \addlyrics differently: the notes of a voice are collected while it is read, and the lyrics are attached to them. With your addlyrics.ly and voice_in_group.ly (including \addlyrics), the output of #201 has the same notes and syllables as your expected files, and it is valid MusicXML 4.0.

I'm happy to go either way:

Whatever is easier for you and the maintainers.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants